package config

import (
	"github.com/pkg/errors"
	"github.com/spf13/viper"
	"github.com/twwch/gin-sdk/twlog"
)

type MongoConf struct {
	K8sAdmin string `json:"k8sAdmin" mapstructure:"k8sAdmin"`
}

type Config struct {
	Port    string        `json:"port" mapstructure:"port"`
	WsPort  string        `json:"ws_port" mapstructure:"ws_port"`
	LogConf twlog.LogConf `json:"logConf" mapstructure:"logConf"`
	Mongo   MongoConf     `json:"mongo" mapstructure:"mongo"`
}

var _globalConfig = new(Config)

func Load(path string) (*Config, error) {
	viper.SetConfigFile(path)
	if err := viper.ReadInConfig(); err != nil {
		return nil, errors.Wrap(err, "failed to read config")
	}
	err := viper.Unmarshal(&_globalConfig)
	if err != nil {
		return nil, errors.Wrap(err, "failed to scan config")
	}
	return _globalConfig, nil
}

func Get() *Config {
	return _globalConfig
}
